/*
 * @Description:
 * @Author: cy2020
 * @Date: 2022-02-23 09:30:29
 * @LastEditTime: 2022-02-25 19:39:16
 */
let md5 = require('md5');

/**
 * 获取设备 认证信息
 * @param {} secureId
 * @param {*} secureKey
 * @returns
 */
let getAuthInfo = (secureId, secureKey) => {
    let now = new Date().getTime(); //当前时间戳
    let username = secureId + '|' + now; // 拼接用户密码
    let password = md5(username + '|' + secureKey); //使用md5生成摘要
    return {
        username,
        password,
    };
};
let logMessage = (message, ...arg) => {
    arg.forEach((_) => {
        let str = typeof _ == 'object' ? JSON.stringify(_) : _;
        message = message.replace('{}', str);
    });
    return message;
};
let getStandTime = () => {
    let nowtimeStr = new Date()
        .toISOString()
        .replace(/T/, ' ') // replace T with a space
        .replace(/\..+/, '');
    return nowtimeStr;
};

// 将字符串切换成数组
let toArr = (str, num) => {
    const arr = [];
    for (let i = 0; i < str.length; i += num) {
        arr.push(str.slice(i, i + num));
    }
    return arr;
};
let strToHex = (params) => {
    if (params == null || params == '' || params == void 0) {
        return false;
    }
    const arr = toArr(params, 16);
    let result = '';
    for (let i = 0; i < arr.length; i++) {
        const itemOne = Buffer.from(arr[i]).toString('hex').toLocaleUpperCase();
        const bufferArr = toArr(itemOne, 2);
        if (bufferArr.length < 16) {
            for (let i = bufferArr.length; i < 16; i++) {
                bufferArr[i] = '  ';
            }
        }
        if (bufferArr.length > 8) {
            bufferArr.splice(8, 0, '   ');
        }
        const bufferItemstr = `[LK-030A] < ${bufferArr.join(' ')} | ${arr[i]}`;
        result += bufferItemstr + '\n';
    }
    console.log(result);
};

/**
 * @name validatorUrl
 * @param {String: url字符串} connectUrl
 * @return {Boolean: 校验结果}
 */
function validatorUrl(connectUrl) {
    let reg =
        /^((http|ws|mqtt|tcp|tl)s?):\/\/[\w-]+(\.[\w-]+)+(:+([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5]))(\/((\.)?(\?)?=?&?[a-zA-Z0-9_-](\?)?)*)*$/i;
    return reg.test(connectUrl);
}

/**
 * @name validatorDeviceName
 * @param {String: url字符串} deviceName
 * @return {Boolean: 校验结果}
 */
// COQ-00-1C3212-SCP52TQ8ME
function validatorDeviceName(deviceName) {
    let reg = /(^COQ)+(-)+([0-9]{2})+(-)+([A-Z0-9]{6})+(-)+([A-Z0-9]{10})$/i;
    return reg.test(deviceName);
}

/**
 * 日志系统
 */
let log = {
    error: (message, ...arg) => {
        console.error(
            '\x1b[41m%s\x1b[0m',
            getStandTime() + ' [INFO]',
            logMessage(message, ...arg)
        );
    },
    warn: (message, ...arg) => {
        console.warn(
            '\x1b[33m%s\x1b[0m',
            getStandTime() + ' [INFO]',
            logMessage(message, ...arg)
        );
    },
    info: (message, ...arg) => {
        console.info(
            '\x1b[36m%s\x1b[0m',
            getStandTime() + ' [INFO]',
            logMessage(message, ...arg)
        );
    },
    /**
     * 列举参数
     */
    table: () => {
        console.table(typeOfConsole);
    },
};
function getKits(){
    var num = '';
    for (var i = 0; i < 10; i++) {
        num += Math.floor(Math.random() * 10);
    }
    return `KIT-${3 + "" + Math.floor(Math.random() * 9)}-000000-` + num;
}
function getCOQ(){
    var num = '';
    for (var i = 0; i < 10; i++) {
        num += Math.floor(Math.random() * 10);
    }
    return "COQ-00-000000-" + num;
}



module.exports = {
    getAuthInfo,
    log,
    validatorUrl,
    validatorDeviceName,
    strToHex,
    getKits,
    getCOQ
};
